package com.calefk.core {
    
    import com.calefk.util.Collection;
    import com.calefk.util.FileLoader;
    import com.calefk.util.Iterator;
    import com.calefk.util.SWFLoader;
    import com.calefk.util.TextLoader;

    public class FileManager {
        
        public static const FILE_SWF:int = 0;
        public static const FILE_XML:int = 1;
        private var mTypeFile:int;
        
        private var mIteratorFile:Iterator;
        private var mFileLoader:FileLoader;
        private var mIsLoadAllFiles:Boolean;
        private var mCollectorFile:Collection;
		// varaible que almacena todos los files loaders 
		private var mInfosLoaders:Array;
        
        private var mTotalPercentAll :Number;
        private var mTotalPercentFile:Number;
        
        private var mCurrentPercentAll:Number = 0;
        private var mPartPercent:Number = 0;
        
        public function get currentPercentLoaded():Number { return mCurrentPercentAll; }
        public function get totalPercent():Number { return mTotalPercentAll; }
        public function get isLoadAllFiles():Boolean { return mIsLoadAllFiles; }
        public function get numFiles():int { return mCollectorFile.size(); }
        
        public function FileManager(typeFile:int , _totalPercentAll:Number = 100) {
            mTypeFile = typeFile;
            mTotalPercentAll = _totalPercentAll;
            mCollectorFile = new Collection();
			
			mInfosLoaders = new Array();
        }
        
        public function loadFiles():void {
            mTotalPercentFile = mTotalPercentAll / mCollectorFile.size();            
            mIteratorFile = new Iterator(mCollectorFile.data.concat());
            
            if (mCollectorFile.size() > 0) {
				mIteratorFile.reset();
                loadCurrentFile();
            } else {
                mIsLoadAllFiles = true;
            }
        }
        
        private function loadCurrentFile():void {
            var info:InfoFile = mIteratorFile.next();
            // instanciando segun el tipo de archivo
            switch (mTypeFile) {
                case FILE_XML:
                    mFileLoader = new TextLoader(info.filePath , TextLoader.TYPE_XML);
                    break;
                case FILE_SWF:
                    mFileLoader = new SWFLoader(info.filePath);
                    break;
            }
            
            mFileLoader.onProgress = onProgress;
            mFileLoader.onLoadComplete = onLoadCompleFile;
			
			/* Cambiando de estado al Info Loader*/
			info.fileLoader = mFileLoader;
			info.state = InfoFile.STATE_LOADING;
			
        }
        
        private function onProgress(bytesLoaded:int , bytesTotal:int):void {
            mCurrentPercentAll = mPartPercent + (bytesLoaded * mTotalPercentFile / bytesTotal);
        }
        
        private function onLoadCompleFile(fileLoader:FileLoader):void {
            // se incrementa las porcentajes
            mPartPercent += mTotalPercentFile;
            mCurrentPercentAll = Math.min(mPartPercent , mTotalPercentAll);
            
            // obtener el siguiente archivo
            var info:InfoFile = mIteratorFile.current();
            
            var objReference:Data = info.objReference;
            objReference.initialize(mFileLoader.document);

            // clean FileLoader
            mFileLoader.clean();
            mFileLoader = null;
			
			/* Cambiando de estado al info */
			info.state = InfoFile.STATE_FINISH;
			
            checkNextLoad();
        }
        
        private function checkNextLoad():void {
            // check is over load Files
            if (mIteratorFile.hasNext()) {
                // Load File
                loadCurrentFile();
            } else {
                onLoadAllFiles();
            }            
        }

        private function onLoadAllFiles():void {
            // override this method
            mIsLoadAllFiles = true;
        }
        
        public function addFile(_filePath:String , _type:int , _objReference:* , _fileId:String = ""):void {
			var info:InfoFile = new InfoFile();
			info.filePath = _filePath;
			info.type = _type;
			info.objReference = _objReference;
			info.id = _fileId;
			info.state = InfoFile.STATE_WAITING;
            mCollectorFile.add(info);
        }
		/**
		 * Busca un FileLoader segun el id asigando en addFile
		 * @param	id
		 * @return FileLoader
		 */
		public function getInfoFile(id:String):InfoFile {
			var size:int = mCollectorFile.data.length;
			for (var k:int = 0; k < size; k++) {
				if (mCollectorFile.data[k].id == id) {
					return mCollectorFile.data[k];
					break;
				}
			}
			return null;
		}
		
		public function clean():void {
			if (mCollectorFile) {
				mCollectorFile.empty();
				mCollectorFile = null;
			}
		}
        
    }

}